#!/bin/sh
#desc:Configure speech settings
#type:local
#package:odspeech

# Copyright(c) 2011 OpenDomo Services SL. Licensed under GPL v3 or later

CFG="/etc/opendomo/speech.conf"
LOGEVENT="/bin/logevent"

SLIST="espeak flite"
# Discard all those synthetizers not installed (not found in /usr/bin)
for p in $SLIST; do
	if test -x /usr/bin/$p; then
		if test -z "$SYNTH"; then
			SYNTH="$p"
		else			
			SYNTH="$SYNTH,$p"
		fi
	fi
done
if test -z "$SYNTH"; then
	echo "#INFO: Sinthetizer not installed"
	echo
	exit
fi


if test -f "$CFG"; then
	# Config found. Load it!
	. $CFG
else
	if ! test -z "$1"; then
		case "$1" in
			espeak)
				# No config found, so use default values
				program="espeak"
				gender="m"  #Config. Can be f or m (male or female)
				variant="1" #Config. Can be number or croak or whisper, or a personalone.
				speed="140" #Config.
				amplitude="10" #Config.
				pitch="50"  #Config.

				echo "program='$program'">$CFG
				echo "gender='$gender'" >> $CFG
				echo "variant='$variant'" >> $CFG
				echo "amplitude='$amplitude'" >> $CFG
				echo "pitch='$pitch'" >> $CFG
				echo "speed='$speed'" >> $CFG
			;;
			flite)
				program=$1
				echo "program='$program'">$CFG
			;;
			*)
				echo "#ERR Invalid TTS"
				$LOGEVENT error odspeech "Configuration failed. Invalid TTS"
				exit 1
			;;
		esac
		$LOGEVENT notice odspeech "Configuration updated"
	else
		echo "#WARN Missing TTS parameter"
	fi
					
fi

# More than 1 params received: we save it.
if ! test -z "$2"; then
	case "$1" in
		espeak)

			if ! test -z "$1"; then
				program=$1
			fi
			if ! test -z "$2"; then
				gender=$2
			fi
			if ! test -z "$3"; then
				variant=$3
			fi
			if ! test -z "$4"; then
				amplitude=$4
			fi
			if ! test -z "$5"; then
				pitch=$5
			fi
			if ! test -z "$6"; then
				speed=$6
			fi

			echo "program='$program'">$CFG
			echo "gender='$gender'" >> $CFG
			echo "variant='$variant'" >> $CFG
			echo "amplitude='$amplitude'" >> $CFG
			echo "pitch='$pitch'" >> $CFG
			echo "speed='$speed'" >> $CFG
		;;
		flite)
			echo "program='$1'">$CFG
			echo "voice='$2'" >> $CFG
		;;
		*)
			echo "#ERR Invalid TTS"
			$LOGEVENT error odspeech "Configuration failed. Invalid TTS."
			exit 1
		;;
	esac
	$LOGEVENT notice odspeech "Configuration updated. Custom values added."
	eh_speech.sh 1 2 "Welcome to OpenDomo" &> /dev/null
	echo "#INFO: Configuration updated!"
	
fi


if ! test -z "$1" || ! test -z "$program"; then
	# If there is a program specified as a param or configured, we continue

	echo "#> Select voice options"
	echo "form:`basename $0`"
	echo "	synth	Sinthetizer	list[$SYNTH]	$program"
	# Specific options for each program:
	case "$program" in
		espeak)
			echo "	gender	Gender	list[m:male,f:female]	$gender"
			echo "	variant	Variant	list[1,2,3,4,5,6,7,8,9]	$variant"
			echo "	amplitude	Amplitude	list[0,5,10,15,20]	$amplitude"
			echo "	pitch	Pitch	list[00,10,20,30,40,50,60,70,80,90]	$pitch"
			echo "	speed	Speed	list[80,160,200]	$speed"
		;;
		flite)
			echo "	voice	Voice	list[voice1,voice2]	$voice"	
		;;
	esac
	# Many params received. Saving configuration
else
	# No params and no config. First menu
	echo "form:`basename $0`"
	echo "	synth	Sinthetizer	list[$SYNTH]"
	echo 
	exit 0
fi
chown admin: ${CFG}
echo
